package com.example.zbh.dsliao.ui.activty;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMGroup;
import com.easemob.chat.EMGroupManager;
import com.easemob.exceptions.EaseMobException;
import com.example.zbh.dsliao.R;
import com.example.zbh.dsliao.model.entity.ContactPeople;
import com.example.zbh.dsliao.ui.adapter.GroupPickContactAdapter;
import com.example.zbh.dsliao.view.FancyIndexer;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by zbh on 15/11/23.
 * 选择群成员
 */
public class GroupPickContactActivity extends BaseActivity implements FancyIndexer.OnTouchLetterChangedListener, AdapterView.OnItemClickListener, View.OnClickListener {
    private TextView add_list_friends;
    private ListView lv_content;
    private FancyIndexer bar;
    private List<String> exitingMembers = new ArrayList<String>();//群中一开始就有成员
    private boolean isCreateNewGroup;
    private List<ContactPeople> listContact = new ArrayList<>();//联系人列表
    private GroupPickContactAdapter adapter;
    private Button add_contact;
    private LinearLayout back;


    @Override
    protected int getLayOut() {
        return R.layout.fragment_contactlist;
    }

    @Override
    protected void initView() {
        add_list_friends = findeView(R.id.add_list_friends);
        lv_content = findeView(R.id.lv_content);
        bar = findeView(R.id.bar);
        add_contact = findeView(R.id.add_contact);
        back = findeView(R.id.back);
    }

    private void getGroupInfo() {
        String groupId = getIntent().getStringExtra("groupId");
        if (groupId == null){
            isCreateNewGroup =true;
        }else {
            EMGroup emGroup = EMGroupManager.getInstance().getGroup(groupId);
            if (emGroup != null){
                exitingMembers = emGroup.getMembers();
            }
        }
        try {
            List<String> friendList = EMChatManager.getInstance().getContactUserNames();
            for (int i = 0; i < friendList.size(); i++) {
                listContact.add(new ContactPeople(friendList.get(i)));
            }
            Collections.sort(listContact);//排序
        } catch (EaseMobException e) {
            e.printStackTrace();
        }


    }

    @Override
    protected void initData() {
        add_contact.setText("保存");
        add_list_friends.setText("选择联系人");
        getGroupInfo();
        adapter = new GroupPickContactAdapter();
        lv_content.setAdapter(adapter);
        adapter.addData(listContact);
        bar.setOnTouchLetterChangedListener(this);
        lv_content.setOnItemClickListener(this);
        add_contact.setOnClickListener(this);
        back.setOnClickListener(this);


    }

    @Override
    public void onTouchLetterChanged(String s) {
        for (int i = 0; i < listContact.size(); i++) {
            Log.i("nan", 6 + "" + s);
            ContactPeople people = listContact.get(i);
            String letter = people.getPinyin().charAt(0) + "";
            Log.i("nannannan", letter);
            if(letter.equals(letter.toLowerCase())){
                letter = letter.toUpperCase();
                Log.i("nannannan", letter);
            }
            if (TextUtils.equals(letter, s)){
                Log.i("nan", letter + "----" + s + i);
                lv_content.setSelection(i);
                break;
            }
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {



    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.add_contact:
                sendDataToNewGroupActivity();
                break;
            case R.id.back:
                finish();
                break;
        }
    }

    private void sendDataToNewGroupActivity() {
    if (getToBeAddMembers().size() == 0){
        Toast.makeText(this,"请选择联系人", Toast.LENGTH_SHORT).show();
    }else {
        Log.i("yyyyyy", getToBeAddMembers().get(0));
        Intent intent = new Intent();
        intent.putExtra("list",getToBeAddMembers().toArray(new String[0]));
        setResult(Activity.RESULT_OK, intent);
        finish();

    }

    }
    private List<String> getToBeAddMembers(){
        return adapter.getAddContactName();
    }
}
